package com.example.demo.admin.sync.cas;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PasswordCheck {
	private static final Logger logger = LoggerFactory.getLogger(PasswordCheck.class);

	public static String entryptPassword(String password, String salt, String encryMode) {
		logger.debug("加密密码");

		try {
			String digestPassword = null;
			if (StringUtils.equalsIgnoreCase(encryMode, encryModeZcjb)) {
				digestPassword = DigestUtils.md5Hex(password);
			} else if (StringUtils.equalsIgnoreCase(encryMode, encryModeBid)) {
				digestPassword = DigestUtils.md5Hex(password);
				if (StringUtils.isNotEmpty(salt)) {
					digestPassword = DigestUtils.md5Hex(digestPassword + salt);
				}
			} else if (StringUtils.equalsIgnoreCase(encryMode, encryModePlat)) {
				byte[] saltByte = Hex.decodeHex(salt.toCharArray());
				byte[] hashPassword = Digests.sha1(password.getBytes(), saltByte, HASH_INTERATIONS);
				digestPassword = Hex.encodeHexString(hashPassword);
			} else {
				digestPassword = DigestUtils.md5Hex(password);
			}

			return digestPassword;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/** 加密方式00，招采进宝-00，投标人服务-10，标准版-20 */
	public static String encryModeZcjb = "00";
	/** 10 **/
	public static String encryModeBid = "10";
	/** 20 **/
	public static String encryModePlat = "20";

	public static int HASH_INTERATIONS = 1024;
}
